< previous page page_79 next page >

Page 79
An Easy Example
An example of a Singleton in your Second Bank of Carrollton Bank Teller System might be the Customer class because your teller user (for simplicity's sake) can handle account transactions for only one customer at a time.
Going with the assumption that your Customer class needs to make sure that only one instance of it can be in memory, your Singleton class would look like Listing 3.1.
LISTING 3.1. THE SINGLETONCustomerCLASS.

9f8044bafea36a1f728739c0bb58ef4c.gif
 1: VERSION 1.0 CLASS
 2: BEGIN
 3:   MultiUse = -1 'True
 4: END
 5: Attribute VB_Name = Customer
 6: Attribute VB_GlobalNameSpace = False
 7: Attribute VB_Creatable = True
 8: Attribute VB_PredeclaredId = False
 9: Attribute VB_Exposed = False
10: Attribute VB_Ext_KEY = SavedWithClassBuilder , Yes
11: Attribute VB_Ext_KEY = Top_Level , Yes
12: 'local variable(s) to hold property value(s)
13: 'Private mvarpSoleInstance As Customer 'local copy
14: Private mvarpSoleInstance As Object 'local copy
15:
16: Friend Property Set pSoleInstance(ByVal vData As Object)
17: 'used when assigning an Object to the property, on the left side
      of a Set statement.
18: 'Syntax: Set x.pSoleInstance = Form1
19:     Set mvarpSoleInstance = vData
20: End Property
21:
22:
23: Friend Property Get pSoleInstance() As Object
24: 'used when retrieving value of a property, on the right side of   
      an assignment.
25: 'Syntax: Debug.Print X.pSoleInstance
26:     Set pSoleInstance = mvarpSoleInstance
27: End Property
28:
29:
30: Friend Static Function createSoleInstance() As Customer
31: Attribute createSoleInstance.VB_UserMemId = 0
32: If TypeName(mvarpSoleInstance) = Customer Then
33:         MsgBox Only one instance allowed
34:         Exit Function
35:     End If
continues

 
< previous page page_79 next page >

If you like this book, buy it!